<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Journal extends AdminController {

	function __construct() { 
		parent::__construct();
        $this -> load -> model("Paper_model");	
		$this -> load -> model("Journal_model");	

		$this -> load -> model("File_model");
		$this -> load -> model("Subject_model");
		$this -> load -> model("Author_model");
		$this -> load -> model("Journal_model");
		$this->load->library('ProjectService/export_service');
		$this->load->library('ProjectService/audit_service');
		$this -> sidebar = "Fruit";
	}

	public function Index() {				
		$data = [];		
		$this -> load -> view('admin/journalList',$data);
	}

	function getDatas($params = array(), $isExport = false) {
		$connar = array();
		$params = $_POST;
		if (!$isExport) {			
			$connar = $this -> Journal_model -> pickPages($connar, $params);
		}

        $connar['orderby'] = "id asc";
        $connar['items']="*";
		// $connar['join']=array('(select group_concat(db_author.author_name ORDER BY id ASC) author,data_id FROM db_author WHERE db_author.type = '.ResultsType::First[XPHEnum::Value].'  GROUP BY db_author.data_id ) author','author.data_id=db_paper.id');
		//$connar['where']=$this->sqlEscape('is_del=?? ',array(CommonWhether::Not [XPHEnum::Value]));

   
		if (isset($params['param'])) {
			$connar['where'] = " name like '%" . $params['param'] . "%' ";
		}

		if (!$isExport) {
			$data = $this -> Journal_model -> get_page_list($connar);
		} else {
			$data["ret"] = $this -> Journal_model -> get_list_full($connar);
		}
      
		foreach ($data['ret'] as $k => $v) {
            $data['ret'][$k]['type'] = (new JournalLevel())->show($v['type']);
        }

        //var_dump($this->db->last_query());
        // var_dump($data);
        // exit();

		if (!$isExport) {
			echo json_encode($data);
		} else {		
			return $data["ret"];			
		}	
	}


	function edit($id){
		if($id == 0){
			$data['title'] = "新增刊物";
			$data['detail']=[];
		}else{
            $data['title'] = "修改刊物";
            $res = $this -> Journal_model -> get_single(array("id" => $id));
            // $res['type'] = (new JournalLevel())->show($res['type']);
            
            $data['detail'] = $res;
        }
        //var_dump($data);
		$this -> load -> view('admin/journalEdit',$data);
	}



	function save(){
        if(empty($_POST['id'])){
            $param = [];
            $param['name'] = $_POST['name'];
            $param['type'] = $_POST['type'];
			$param['start_time'] = $_POST['start_time'];
            $param['end_time'] = $_POST['end_time'];
			$result = $this -> Journal_model -> add($param);
        }else{         
			$sql = "select * from db_journal where id = ".$_POST['id'];
			$res = $this -> Journal_model -> query($sql);
			$journal_level= (new JournalLevel())->show($res[0]['type']);
			$journal_name = $res[0]['name'];
			$publish_time = strtotime($res[0]['start_time']);
			$journal_level2= (new JournalLevel())->show($_POST['type']);

			$param = [];
            $param['id'] = $_POST['id'];
            $param['name'] = $_POST['name'];
            $param['type'] = $_POST['type'];
			$param['start_time'] = $_POST['start_time'];
            $param['end_time'] = $_POST['end_time'];
			$result = $this -> Journal_model -> update($param,$key="id");

			$result = $this->db->query("update db_paper set journal_level= '$journal_level2' where journal_name= '$journal_name' and journal_level= '$journal_level' and publish_time>= '$publish_time' ");
        }

		if(!($result)){
			exit(retJson('保存失败！',false));
		}
		else{
			exit(retJson('保存成功！',true));
		}		
	}

	

	//删除

	// function delete($id){
	// 	$list = $this -> Paper_model -> get_single(array("id" => $id));
	// 	if($list){
	// 		$param['id'] = $id;
	// 		$param['is_del'] = CommonWhether::Whether [XPHEnum::Value];//已删除
	// 		$this -> Paper_model -> update($param);
	// 	}
	// 		exit(retJson("删除成功.", true));
			
	// }




}